﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.Controllers;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;
	using Falcon.Forums.Domain;
	using Falcon.Forums.Domain.Extensions;
	using Falcon.Forums.Domain.Services;
	using Falcon.Forums.Domain.Values;

	public class SystemForumAnnouncementController : DefaultController
	{
		public SystemForumAnnouncementController(
			IForumService forumService, 
			IForumAnnouncementService forumAnnouncementService)
		{
			this.ForumService = forumService;
			this.ForumAnnouncementService = forumAnnouncementService;
		}

		public IForumService ForumService
		{
			get;
			private set;
		}

		public IForumAnnouncementService ForumAnnouncementService
		{
			get; 
			private set;
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Index()
		{
			// get all the announcements
			var announcements = ForumAnnouncementService.GetAll();

			// get all the forums
			var forums = ForumService.GetAll(new ForumSpecification
			{
				SkipPermissions = true
			});

			// group the announcements by global and forum
			var grouped = announcements.ToGrouped(forums);

			// render the view
			return View(Views.Index, Masters.System, grouped);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Detail(int id, string slug)
		{
			var announcement = ForumAnnouncementService.GetById(id);

			return announcement == null ? Error(ForumsResources.Messages.ForumAnnouncementNotFound) : View(Views.Detail, Masters.System, announcement);
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Create(int? id)
		{
			var announcement = ForumAnnouncementService.Create();

			return announcement.CanCreate ? View(Views.Create, Masters.System, new ForumAnnouncementCreate(id)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post), Authorize]
		public ActionResult Create(ForumAnnouncementCreate value)
		{
			try
			{
				var announcement = ForumAnnouncementService.Create();

				if (!announcement.CanCreate)
				{
					return NotAuthorized();
				}

				this.ForumAnnouncementService.Insert(announcement, value);

				return Message(ForumsRoutes.SystemForumAnnouncementUpdate, new { id = announcement.Id }, ForumsResources.Messages.ForumAnnouncementCreated.Fmt(announcement.Title));
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Create, Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Update(int id)
		{
			var announcement = ForumAnnouncementService.GetById(id);

			if (announcement == null)
			{
				return Error(ForumsResources.Messages.ForumAnnouncementNotFound);
			}

			return announcement.CanUpdate || announcement.CanUpdateAny ? View(Views.Update, Masters.System, new ForumAnnouncementUpdate(announcement)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post), Authorize]
		public ActionResult Update(ForumAnnouncementUpdate value)
		{
			try
			{
				var announcement = ForumAnnouncementService.GetById(value.Id);

				if (announcement == null)
				{
					return Error(ForumsResources.Messages.ForumAnnouncementNotFound);
				}

				if (!announcement.CanUpdate)
				{
					return NotAuthorized();
				}

				this.ForumAnnouncementService.Update(announcement, value);

				return Message(ForumsRoutes.SystemForumAnnouncementUpdate, new { id = announcement.Id }, ForumsResources.Messages.ForumAnnouncementUpdated.Fmt(announcement.Title));
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Update, Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Delete(int id)
		{
			var announcement = this.ForumAnnouncementService.GetById(id);

			if (announcement == null)
			{
				return base.Error(ForumsResources.Messages.ForumAnnouncementNotFound);
			}

			return announcement.CanDelete ? base.View(Views.Delete, Masters.System, new ForumAnnouncementDelete(announcement)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post), Authorize]
		public ActionResult Delete(ForumAnnouncementDelete value)
		{
			try
			{
				var announcement = this.ForumAnnouncementService.GetById(value.Id);

				if (announcement == null)
				{
					return base.Error(ForumsResources.Messages.ForumAnnouncementNotFound);
				}

				if (!announcement.CanDelete)
				{
					return NotAuthorized();
				}

				this.ForumAnnouncementService.Delete(announcement);

				return base.RedirectToRoute(ForumsRoutes.SystemForumAnnouncementIndex);
			}
			catch (ValidationException exception)
			{
				base.ModelState.CopyValidationExceptions(exception);
			}

			return base.View(Views.Delete, Masters.System, value);
		}
	}
}